<?php 
class AdminController extends Controller
{
    public function index()
    {   
        if (empty($_SESSION['liaoliao_admin'])) {
            header("location:./index.php?route=admin/login");
            exit();
        }
        $siteurl=$this->getCfg('siteurl');
        $this->assign('siteurl',$siteurl);
        $mysqlversion=$this->db->getDbVerion();
        $this->assign("mysqlversion",$mysqlversion);
        $this->render();
    }

    public function login()
    {
        if (!empty($_SESSION['liaoliao_admin'])) {
            header("location:./index.php?route=admin/index");
            exit();
        }
        $this->render(false);
    }

    public function dologin()
    {
        $key=require_once('config/key.php');
        require_once('config/encrypt.php');
        $username=addslashes($_POST['username']);
        $password=addslashes($_POST['password']);
        $password=cc_encrypt($password,$key);
        $res=$this->db->find('liaoliao_admin','username='.'"'.$username.'"'.' and password='.'"'.$password.'"');
        if ($res) {
            $_SESSION['liaoliao_admin']=$res['username'];
            $data=['status'=>1,'username'=>$res['username']];
            echo json_encode($data,true);
        }else{
            $data=['status'=>0,'message'=>'不存在此账号'];
            echo json_encode($data,true);
        }
    }

    public function logout()
    {
        if (empty($_SESSION['liaoliao_admin'])) {
            header("location:./index.php?route=admin/login");
            exit;
        }
        unset($_SESSION['liaoliao_admin']);
        header("location:./index.php?route=admin/login");
    }

    public function tags()
    {
        if (empty($_SESSION['liaoliao_admin'])) {
            header("location:./index.php?route=admin/login");
            exit;
        }
        $siteurl=$this->getCfg('siteurl');
        $this->assign('siteurl',$siteurl);
        require "class/Fenye.class.php";
        $count=$this->db->getRow("select count(*) from liaoliao_tags");
        $count=$count["count(*)"];
        $fenye=new Fenye($count,5);
        $show=$fenye->show();
        $sql=$fenye->listcon("select * from liaoliao_tags order by sort desc,id desc");
        $tags=$this->db->getAll($sql);
        if ($tags) {
            $this->assign('tags',$tags);
            $this->assign('show',$show);
        }
        $this->render();
    }

    public function addtag()
    {
        if (empty($_SESSION['liaoliao_admin'])) {
            header('location:./index.php?route=admin/login');
            exit;
        }
        $this->render(false);
    }

    public function doaddtag()
    {
        if (empty($_SESSION['liaoliao_admin'])) {
            header("location:./index.php?route=admin/login");
            exit;
        }
        $name = addslashes(htmlspecialchars($_POST['tagname']));
        $description = addslashes(htmlspecialchars($_POST['tagdesc']));
        $sort=intval($_POST['tagsort']);
        if (!empty($name) && !empty($description)) {
            // $rel = $this->db->exec("insert into liaoliao_tags(name,description,sort) values('" . $tagname . "','" . $tagdesc . "','" . $tagsort . "')");
            $data=compact('name','description','sort');
            $res=$this->db->add($data,'liaoliao_tags');
            if ($res) {
                $res=['status'=>1,'message'=>'添加成功'];
                echo json_encode($res);
            }else{
                $res=['status'=>0,'message'=>'新增失败'];
                echo json_encode($res);
            }
        }else{
            echo json_encode(array('status'=>0,'message'=>'请填写表单'));
        } 

    }


}